<template>
  <div class="payBox">
    用餐消费总额：{{amount}}元
    <input type="number" v-model="amount" v-focus step="0.01" />
    <div class="user">
      <input type="text" v-model="newPerson" @keyup.enter="addPerson" />
      <button @click="addPerson">新增用餐人数</button>
    </div>
    <div class="allInfo" v-if="List.length!=0">
      <div class="personInfo" v-for="(item,index) in List" :key="index">
        {{item}}应该支付{{(amount/List.length).toFixed(2)}}元
        <button @click="delectPerson(index)">删除</button>
      </div>
    </div>
    <div v-else-if="List.length==0">请输入用餐人数</div>
    <!-- <div>平均每人应付款：{{(amount/List.length).toFixed(2)}}</div> -->
  </div>
</template>

<script>
export default {
  data() {
    return {
      amount: 0,
      newPerson: "",
      List: [],
    };
  },
  methods: {
    // 删除人数
    delectPerson(index) {
      this.List.splice(index, 1);
    },
    // 增加人数
    addPerson() {
      this.List.push(this.newPerson);
      //   点击新增人数的按钮后输入框不显示数据
      this.newPerson = "";
      //   console.log(this.newPerson);
      //   console.log(this.List);
    },
    // watch: {
    //   List() {},
    // },
  },
  //   自定义指令
  directives: {
    //  自动聚焦
    focus: {
      inserted(dom) {
        dom.focus();
      },
    },
  },
  //   watch: {
  //     List,
  //   },
};
</script>

<style lang="less">
.payBox {
  width: 267px;
  margin: 30px;
  /* border: 1px solid #000; */
}
.allInfo {
  width: 260px;
  margin: 10px 0;
  border: 1px solid #000;
}
.personInfo {
  margin: 10px;
}
.user {
  margin-top: 10px;
}
</style>